package com.etoak.common.web.context;

import com.etoak.common.jwt.MyJwtUtil;
import com.etoak.common.web.util.ServletUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;
import java.util.Objects;

public class LoginUserContext {

    /**
     * 获取登录用户的id
     */
    public static String getId() {
        String token = ServletUtil.getRequest().getHeader(HttpHeaders.AUTHORIZATION);
        Map<String, Object> claims = MyJwtUtil.parse(token);
        return claims.get("id").toString();
    }

    /**
     * 获取 HttpServletRequest 对象
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes.getRequest();
    }
}
